package com.cloud.auth.security;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.cloud.auth.dao.RoleDao;
import com.cloud.auth.dao.UserDao;
import com.cloud.auth.entity.Role;
import com.cloud.auth.entity.User;



@Service
public class MyUserDetailService implements UserDetailsService{
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private RoleDao roleDao;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User u=userDao.findOneByUsername(username);
		List<Role> roleList= roleDao.findRoleByUsername(username);
		Role r=roleList.get(0);//角色级，获取所有权限列表后，使用MyUserPrincipal另一构造方法可以作为权限级别的粒度
		return new MyUserPrincipal(u, r);
	}

}
